package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.PubSub.MethodSubscriber;
import yxy.apple.util.PubSub.SubscriptionMethod;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.pubsub.ActivityPublisher;
import yxy.game.pm2.bean.pubsub.ActivityPublisher.ActivitySubscription;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class ActiveSubscriber_招募令礼包 extends MethodSubscriber {

    static private ActiveSubscriber_招募令礼包 instance;

    public static ActiveSubscriber_招募令礼包 getInstance() {
        if (instance == null) {
            instance = new ActiveSubscriber_招募令礼包();
            instance.init();
        }
        return instance;
    }

    private void init() {
        ActivityPublisher.getInstance().subscribe(this);
    }

    private ActiveSubscriber_招募令礼包() {

    }

    @SubscriptionMethod(Type = ActivitySubscription.class)
    public void receive(ActivitySubscription subscription) {
        String playerCode = subscription.getPlayerCode();
        Currency currency = subscription.getCurrency();
        CCReason reason = subscription.getReason();

        //高级招募令
        if (currency.getDataId() == 1002) {
            if (reason == CCReason.招募武将) {
                ActiveModule_招募令礼包 am = ActiveModule_招募令礼包.get(playerCode);
                if (am == null) {
                    return;
                }
                //当天只发送一次
                if (am.getStartTime() == null || isNextDay(am)) {
                    am.setStartTime(DateTime.now());
                    am.set();
                    am.createRecruitPackage(currency.getNumber());
                }
            }
        }

    }

    //判断是否下一天
    public Boolean isNextDay(ActiveModule_招募令礼包 am) {
        Date date = am.getStartTime().toDate();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1; //当前月
        int day = instance.get(Calendar.DAY_OF_MONTH);
        LocalDate startDate = LocalDate.of(year, month, day);
        long betweenDay = ChronoUnit.DAYS.between(startDate, LocalDate.now());
        return betweenDay >= 1;
    }
}
